import {useCountStore} from '@/stores/copyStore'
import { VueFlow, useVueFlow, type GraphEdge, type GraphNode } from '@vue-flow/core'
function copySelected(selectedNodes:Array<GraphNode>, selectedEdges:Array<GraphEdge>) {
  const copyStore = useCountStore()
  console.log('copySelected', selectedNodes, selectedEdges)
  copyStore.selectedNodes = selectedNodes
  copyStore.selectedEdges = selectedEdges
}
let newEdges = Array<GraphNode>()
function getPasteNodesAndEdges(): {nodes:Array<GraphNode>,edges:Array<GraphEdge>}{
  const copyStore = useCountStore()
  console.log('pasteSelected', copyStore.selectedNodes, copyStore.selectedEdges)  
  const newNodes = Array<GraphNode>()
  const nodeIdChange = <{[key: string]: string}>{}
  const newEdges = Array<GraphEdge>()
  for (const node of copyStore.selectedNodes) {
    const copynode: typeof node = JSON.parse(JSON.stringify(node));
    copynode.id = Math.random().toString(36).substring(2, 9)
    nodeIdChange[node.id] = copynode.id
    copynode.position = {
      x: copynode.position.x + 100,
      y: copynode.position.y + 100,
    }
    newNodes.push(copynode)
  }
  for (const edge of copyStore.selectedEdges){
    const copyedge: typeof edge = JSON.parse(JSON.stringify(edge));
    copyedge.id = Math.random().toString(36).substring(2, 9)
    if (!(copyedge.source in nodeIdChange))continue
    if (!(copyedge.target in nodeIdChange))continue
    copyedge.source = nodeIdChange[copyedge.source]
    copyedge.target = nodeIdChange[copyedge.target]
    newEdges.push(copyedge)
  }
  return {nodes:newNodes,edges:newEdges}
}
export const useCopySelected = () => {
  return { copySelected,  getPasteNodesAndEdges}
}
export default useCopySelected;
